<?php
/**
 * 借款业务管理
 */
namespace Admin\Controller\Borrow;
use Think\Controller;
use Admin\Controller\Common\BaseController;
use Think\Model;

class BorrowbusinessController extends BaseController
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
//	public function _initialize()
//	{
//		session('facId','51');
//		// 判断用户是否登陆
//		if (!session('?facId'))
//		{
//			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
//			redirect($url);
//			exit;
//		}
//	}


	/**
	 *	借款业务列表页
	 */
	public function borrowbusiness_list() 
	{
//		$select_borrowNum = I("post.select_borrowNum","");
//		$select_custName = I("post.select_custName","");
//		$select_checkState = I("post.select_checkState","");
//		$select_time = I("post.select_time","");

//		$search_array['select_borrowNum'] = $select_borrowNum;
//		$search_array['select_custName'] = $select_custName;
//		$search_array['select_checkState'] = $select_checkState;
//		$search_array['select_time'] = $select_time;
//
//		$this->assign("search_array",$search_array);

//		$model_buinessinfo = M('bypal_borrow_buinessinfo');
//		if ($select_checkState != '')
//		{
//			$array_buinessinfo = $model_buinessinfo->where('binfo_state="'.$select_checkState.'"')->select();
//		}
//		else
//		{
//			$array_buinessinfo = $model_buinessinfo->select();
//		}
//		$array_id = array('0');
//		foreach ($array_buinessinfo as $key => $value)
//		{
//			array_push($array_id, $value['binfo_borrowid']);
//		}
//
//		$model = M('bypal_borrow_info');
//		if ($select_borrowNum != '')
//		{
//			$condition['borrow_id'] = array(array('like','%'.$select_borrowNum.'%'),array('in', $array_id));
//		}
//		else
//		{
//			$condition['borrow_id'] = array('in', $array_id);
//		}
//		if ($select_custName != '')
//		{
//			$model_custInfo = M('bypal_cust_info');
//			$condition_custInfo['cust_name'] = array('like','%'.$select_custName.'%');
//			$array_custInfo = $model_custInfo->where($condition_custInfo)->select();
//			$array_custInfo_exist = array('0');
//			foreach ($array_custInfo as $key => $value)
//			{
//				array_push($array_custInfo_exist, $value['cust_id']);
//			}
//			$condition['borrow_custid'] = array('in', $array_custInfo_exist);
//		}
//		if ($select_time != '')
//		{
//			$condition['borrow_createdate'] = array(array('egt',strtotime($select_time)),array('lt',strtotime($select_time)+86400));
//		}
//		$array_data = $model->where($condition)->order('borrow_passtime desc')->select();
//		$sum_interest = 0;
//		$sum_unmoney = 0;
//		$sum_interest_un = 0;
//		$sum_actual_pricipal = 0;
//		foreach ($array_data as $key => $value)
//		{
//			$model_facilitatorInfo = M('bypal_facilitator_info');
//			$data_facilitatorInfo = $model_facilitatorInfo->where('id='.$value['borrow_busid'])->find();
//			$array_data[$key]['fac_simple_name'] = $data_facilitatorInfo['fac_simple_name'];
//
//			$model_custInfo = M('bypal_cust_info');
//			$data_custInfo = $model_custInfo->where('cust_id='.$value['borrow_custid'])->find();
//			$array_data[$key]['cust_name'] = $data_custInfo['cust_name'];
//
//			$model_itype = M('bypal_facilitator_itype');
//			$data_itype = $model_itype->where('trade_number="'.$data_facilitatorInfo['fac_separatetype'].'"')->find();
//			$array_data[$key]['itype_name'] = $data_itype['name'];
//
//			$array_data[$key]['borrow_passtime'] = date('Y-m-d', $value['borrow_passtime']);
//
//			$model_buinessinfo = M('bypal_borrow_buinessinfo');
//			$data_buinessinfo = $model_buinessinfo->where('binfo_borrowid="'.$value['borrow_id'].'"')->find();
//			$model_borrowStatus = M('bypal_borrow_status');
//			$data_borrowStatus = $model_borrowStatus->where('bsta_id="'.$data_buinessinfo['binfo_state'].'"')->find();
//			$array_data[$key]['binfo_state_name'] = $data_borrowStatus['bsta_name'];
//
//			$model_repay_detail = M('bypal_repay_detail');
//			$sum_repay_detail_interest = $model_repay_detail->where('binfo_id="'.$value['borrow_id'].'"')->sum('interest');
//			$sum_repay_detail_actual_interest = $model_repay_detail->where('binfo_id="'.$value['borrow_id'].'"')->sum('actual_interest');
//			$sum_repay_detail_actual_pricipal = $model_repay_detail->where('binfo_id="'.$value['borrow_id'].'"')->sum('actual_pricipal');
//			$sum_actual_pricipal += $sum_repay_detail_actual_pricipal;
//
//			//	总利息
//			$sum_interest += $sum_repay_detail_interest;
//			//	存量利息
//			$sum_interest_un += $sum_repay_detail_interest - $sum_repay_detail_actual_interest;
//
//			if ($value['borrow_investid'] == '0')
//			{
//				$array_data[$key]['check_invest'] = '否';
//			}
//			else
//			{
//				$array_data[$key]['check_invest'] = '是';
//			}
//
//			if ($data_buinessinfo['binfo_state'] == '1')
//			{
//				$array_data[$key]['check_payed'] = '未放款';
//				$sum_unmoney += $value['borrow_money'];
//			}
//			elseif ($data_buinessinfo['binfo_state'] == '3')
//			{
//				$array_data[$key]['check_payed'] = '已放款';
//			}
//			elseif ($data_buinessinfo['binfo_state'] == '4')
//			{
//				$array_data[$key]['check_payed'] = '已完成';
//			}
//			elseif ($data_buinessinfo['binfo_state'] == '5')
//			{
//				$array_data[$key]['check_payed'] = '已取消';
//			}
//            $array_data[$key]['binfo_state']=$data_buinessinfo['binfo_state'];
//		}
//
//		$json_list = json_encode($array_data);

//		$sum_money1 = $model->where($condition)->sum('borrow_money');
//		//	总本金
//		$sum_money = $sum_money1 - $sum_unmoney;
//		//	存量本金
//		$sum_money_un += $sum_money - $sum_actual_pricipal;

//		$array_sum = array();
//		$array_sum[0]['fac_simple_name'] = '总本金:';
//		$array_sum[0]['borrow_money'] = $sum_money . ' 元';
//		$array_sum[0]['borrow_loanperiod'] = '总利息:';
//		$array_sum[0]['cust_name'] = $sum_interest . ' 元';
//		$array_sum[0]['binfo_state_name'] = '总额:';
//		$array_sum[0]['check_invest'] = $sum_money + $sum_interest . ' 元';
//		$array_sum[1]['fac_simple_name'] = '存量本金:';
//		$array_sum[1]['borrow_money'] = $sum_money_un . ' 元';
//		$array_sum[1]['borrow_loanperiod'] = '存量利息:';
//		$array_sum[1]['cust_name'] = $sum_interest_un . ' 元';
//		$array_sum[1]['binfo_state_name'] = '存量总额:';
//		$array_sum[1]['check_invest'] = $sum_money_un + $sum_interest_un . ' 元';
//		$json_sum = json_encode($array_sum);

//		$json =  '{"total":' . count($array_data) . ',"rows":' . $json_list . ',"footer":' . $json_sum . '}';
//		$this->assign('json_list',$json);

		//	借款状态下拉框数据
		$model_state = M('bypal_borrow_status');
		$array_state = $model_state->select();
		$this->assign('array_state',$array_state);
		$this->display();
	}

    public function borrowbusiness_main(){
        $page=I("post.page","1");
        $rows=I("post.rows","10");
        $select_borrowNum = I("post.select_borrowNum","");
		$select_custName = I("post.select_custName","");
        $select_delayStatus=I("post.select_delayStatus","");
		$select_borrowStatus = I("post.select_borrowStatus","");
		$select_start_time = I("post.select_start_time","");
        $select_end_time = I("post.select_end_time","");

		$search_array['select_borrowNum'] = $select_borrowNum;
		$search_array['select_custName'] = $select_custName;
        $search_array['$select_delayStatus']=$select_delayStatus;
		$search_array['select_borrowStatus'] = $select_borrowStatus;
		$search_array['select_start_time'] = $select_start_time;
        $search_array['select_end_time'] = $select_end_time;

		$this->assign("search_array",$search_array);

        $borrow_business=D("BorrowBuinessinfo");
        $borrow_array=$borrow_business->borrow_business($search_array,$page,$rows);
        echo json_encode($borrow_array);
    }


	/**
	 *	借款业务列表页#####放款操作
	 */
	public function borrowbusiness_list_paymoney() 
	{
		if ($_POST && array_key_exists('borrow_id', $_POST) && $_POST['borrow_id']) 
		{
			$model_borrow_info = M('bypal_borrow_info');
			$data_borrow_info = $model_borrow_info->where('borrow_id="'.$_POST['borrow_id'].'"')->find();

			$model_facilitator_trade = M('bypal_facilitator_trade');
			$data_facilitator_trade = $model_facilitator_trade->where('trade_contractnumber="'.$_POST['borrow_id'].'"')->find();

			$time = time();
			$time2 = strtotime(date("Y-m-d",time()));
			$num = $data_borrow_info['borrow_loanperiod'] + $data_facilitator_trade['delay_time'];
			$finaltime = $this->addOneMonth($time,$num);

			$model_borrow_buinessinfo = M('bypal_borrow_buinessinfo');
			$update_borrow_buinessinfo['buinfo_contractwork'] = $time;
			$update_borrow_buinessinfo['binfo_opendate'] = $time;
			$update_borrow_buinessinfo['binfo_expiredate'] = $finaltime;
			$update_borrow_buinessinfo['buinfo_contractdown'] = $finaltime;
			$update_borrow_buinessinfo['binfo_finalpaydate'] = $time;
			$update_borrow_buinessinfo['binfo_firstpaydate'] = $time;
			$update_borrow_buinessinfo['binfo_state'] = '3';
			$model_borrow_buinessinfo->where('binfo_borrowid="'.$_POST['borrow_id'].'"')->save($update_borrow_buinessinfo);

			$update_facilitator_trade['trade_fptime'] = $time;
			$update_facilitator_trade['trade_lptime'] = $time;
			$update_facilitator_trade['pid'] = '3';
			$update_facilitator_trade['tid'] = '6';
			$model_facilitator_trade->where('trade_contractnumber="'.$_POST['borrow_id'].'"')->save($update_facilitator_trade);

			$model_repay_detail = M('bypal_repay_detail');
			$principal = round($data_borrow_info['borrow_money']/$data_borrow_info['borrow_loanperiod'],2);
			$interest = round($data_borrow_info['borrow_money']*($data_borrow_info['borrow_rate']/1200),2);
			$allinterest = round($data_borrow_info['borrow_loanperiod']*$data_borrow_info['borrow_money']*($data_borrow_info['borrow_rate']/1200),2);
			$poundage = 0;
			for ($i=1; $i <= $data_borrow_info['borrow_loanperiod']; $i++) 
			{ 
				$start_num = $data_facilitator_trade['delay_time']+$i-1;
				$one_day = 3600*24;
				$start_date = $this->addOneMonth($time2,$start_num)+$one_day;
				$end_date = $this->addOneMonth($time2,$start_num+1);

				if ($i == $data_borrow_info['borrow_loanperiod']) 
				{
					$principal = round($data_borrow_info['borrow_money']-($principal*($data_borrow_info['borrow_loanperiod']-1)),2);
					$interest = round($allinterest-($interest*($data_borrow_info['borrow_loanperiod']-1)),2);
				}

				if($i == 1) 
				{
					$poundage = $data_borrow_info['borrow_extramoney']+$data_facilitator_trade['service_money'];
				}
				else
				{
					$poundage = $data_facilitator_trade['service_money'];
				}

				$insert_repay_detail['binfo_id'] = $_POST['borrow_id'];
				$insert_repay_detail['period_number'] = $i;
				$insert_repay_detail['start_date'] = $start_date;
				$insert_repay_detail['end_date'] = $end_date;
				$insert_repay_detail['principal'] = $principal;
				$insert_repay_detail['interest'] = $interest;
				$insert_repay_detail['poundage'] = $poundage;
				$insert_repay_detail['period_recovery'] = '0';
				$insert_repay_detail['cumulative_recovery'] = '0';
				$insert_repay_detail['repay_date'] = '0';
				$model_repay_detail->add($insert_repay_detail);
			}

			$result['state'] = '1';
			$result['message'] = "放款成功！";

			$this->ajaxReturn(json_encode($result));
		}
	}


	public function addOneMonth($time,$i) 
	{
		$tmp_date = strtotime('+'.$i.' month',$time);
		$tmp_month = date("m",$time)+$i-date("m",$tmp_date)+12*(date("Y",$time)-date("Y",$tmp_date));
		if($tmp_month == 0) 
		{
			return $tmp_date;
		}
		else
		{
			return strtotime("-1 day",strtotime(date("Y-m-01",$tmp_date)));
		}
	}


	/**
	 *	借款业务详情
	 */
	public function borrowbusiness_detail() 
	{
        if(IS_GET&&I("get.borrow_id",'','/^\d+$/')!=''){
            $borrow_id=I("get.borrow_id");
            $this->assign("borrow_id",$borrow_id);
            $this->display();
        }else{
            $this->error('参数错误','/Article/error');
        }

        exit;
		if (IS_GET && I('get.borrow_id')) 
		{
			$model = M('bypal_borrow_buinessinfo');
			$data = $model->where('binfo_borrowid='.I('get.borrow_id'))->find();

			$model_borrow_info = M('bypal_borrow_info');
			$data_borrow_info = $model_borrow_info->where('borrow_id='.$data['binfo_borrowid'])->find();

			//	标题
			$data['borrow_title'] = $data_borrow_info['borrow_title'];

			//	商户ID
			$data['borrow_busid'] = $data_borrow_info['borrow_busid'];

			//	客户ID
			$data['borrow_custid'] = $data_borrow_info['borrow_custid'];

			//	金额
			$data['borrow_money'] = $data_borrow_info['borrow_money'];

			//	利率
			$data['borrow_rate'] = $data_borrow_info['borrow_rate'];

			//	期限
			$data['borrow_loanperiod'] = $data_borrow_info['borrow_loanperiod'];

			//	手续费
			$data['borrow_extramoney'] = $data_borrow_info['borrow_extramoney'];

			//	申请状态ID
			$data['borrow_stateid'] = $data_borrow_info['borrow_stateid'];

			//	取消时间
			if ($data_borrow_info['borrow_unworktime'] == '0') 
			{
				$data['borrow_unworktime'] = '未取消';
			}
			else
			{
				$data['borrow_unworktime'] = date('Y-m-d H:i:s', $data_borrow_info['borrow_unworktime']);
			}
			
			//	商户全称
			$model_facilitatorInfo = M('bypal_facilitator_info');
			$data_facilitatorInfo = $model_facilitatorInfo->where('id='.$data_borrow_info['borrow_busid'])->find();
			$data['fac_name'] = $data_facilitatorInfo['fac_name'];

			//	商户联系人
			$data['fac_contact'] = $data_facilitatorInfo['fac_contact'];

			//	商户联系电话
			$data['fac_contactphone'] = $data_facilitatorInfo['fac_contactphone'];

			//	首付比例
			$model_facilitator_trade = M('bypal_facilitator_trade');
			$data_facilitator_trade = $model_facilitator_trade->where('trade_contractnumber='.$data['binfo_borrowid'])->find();
			$data['trade_perofpay'] = $data_facilitator_trade['trade_perofpay'];

			//	延期月数
			$data['delay_time'] = $data_facilitator_trade['delay_time'];

			//	剩余分期额度
			$array_data1 = $model_borrow_info->where('borrow_custid='.$data_borrow_info['borrow_custid'])->select();
			$array_exist = array('0');
			foreach ($array_data1 as $k => $val) 
			{
				array_push($array_exist, $val['borrow_id']);
			}
			$condition1['binfo_id'] = array('in', $array_exist);
			$condition1['repay_date'] = '0';
			$model_repayDetail = M('bypal_repay_detail');
			$sum = $model_repayDetail->where($condition1)->sum('principal');
			$data['sum'] = round(50000-$sum,2);

			//	还款信息详情表格数组
			$array_repay_detail = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'"')->order('period_number asc')->select();
			$this->assign('array_repay_detail',$array_repay_detail);

			//	已付滞纳金
			$sum_demurrage = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date!="0"')->sum('demurrage');

			//	已付罚金
			$sum_fine_money = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date!="0"')->sum('fine_money');

			//	已还本金
			$sum_principal = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date!="0"')->sum('principal');
			$data['sum_principal'] = round($sum_principal, 2);

			//	已还总额
			$sum_period_recovery = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date!="0"')->sum('period_recovery');

			//	已还利息
			$data['repay_interest'] = round($sum_period_recovery - $sum_principal, 2);

			//	未还本金
			$sum_principal2 = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date="0"')->sum('principal');
			$data['sum_principal2'] = round($sum_principal2, 2);

			//	未还利息
			$sum_interest = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date="0"')->sum('interest');
			$data['sum_interest'] = round($sum_interest, 2);

			//	未付手续费
			$sum_poundage = $model_repayDetail->where('binfo_id="'.$data['binfo_borrowid'].'" and repay_date="0"')->sum('poundage');

			//	已付其他费用
			$data['other_money'] = round($sum_demurrage + $sum_fine_money, 2);

			//	应还总额
			$data['total_repay_money'] = round($sum_period_recovery + $sum_principal2 + $sum_interest + $sum_poundage, 2);

			//	已还总额
			$data['sum_period_recovery'] = round($sum_period_recovery, 2);

			//	未还总额
			$data['un_repay_money'] = round($sum_principal2 + $sum_interest + $sum_poundage, 2);

			//	商户类别
			$model_management_type = M('bypal_management_type');
			$data_management_type = $model_management_type->where('man_type_id='.$data_facilitatorInfo['man_type_id'])->find();
			$data['man_type'] = $data_management_type['man_type'];

			//	借款等级
			$model_borrow_adjustvalue = M('bypal_borrow_adjustvalue');
			$data_borrow_adjustvalue = $model_borrow_adjustvalue->where('ad_id='.$data_borrow_info['borrow_level'])->find();
			$data['ad_level'] = $data_borrow_adjustvalue['ad_level'];

			//	服务分类
			$model_facilitator_itype = M('bypal_facilitator_itype');
			$data_facilitator_itype = $model_facilitator_itype->where('trade_number="'.$data_facilitatorInfo['fac_separatetype'].'"')->find();
			$data['service_type'] = $data_facilitator_itype['name'];

			//	客户姓名
			$model_cust_info = M('bypal_cust_info');
			$data_cust_info = $model_cust_info->where('cust_id='.$data_borrow_info['borrow_custid'])->find();
			$data['cust_name'] = $data_cust_info['cust_name'];

			//	客户电话
			$data['cust_phone'] = $data_cust_info['cust_phone'];

			//	借款用途
			$model_cust_loantype = M('bypal_cust_loantype');
			$data_cust_loantype = $model_cust_loantype->where('lt_id='.$data_borrow_info['borrow_applyid'])->find();
			$data['lt_value'] = $data_cust_loantype['lt_value'];

			//	还款方式
			$model_repay_method = M('bypal_repay_method');
			$data_repay_method = $model_repay_method->where('repay_id='.$data_borrow_info['borrow_repayid'])->find();
			$data['repay_name'] = $data_repay_method['repay_name'];

			//	账户余额
			$model_balance_invest = M('bypal_balance_invest');
			$data_balance_invest = $model_balance_invest->where('cust_id='.$data_borrow_info['borrow_custid'])->find();
			$data['fund_balance'] = $data_balance_invest['fund_balance'];

			$this->assign('data',$data);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}

    /**
     * 分期借款业务详情， 订单信息 商户信息，个人账户信息，订单信息
     */
    public function ajax_borrow_info_detail(){
        if(IS_POST&&I("post.borrow_id",'','/^\d+$/')!=''){
            $borrow_id=I("post.borrow_id");
            $borrowinfo=D("BorrowBuinessinfo");

            $borrow_array=$borrowinfo->borrow_info_fac_detail($borrow_id);
            $this->ajaxReturn($borrow_array);

        }
    }

    /**
     * 分期借款业务详情， 订单信息 未还部分_已还_总还 资料
     */
    public function ajax_borrow_info_detail_repaid_info(){
        if(IS_POST&&I("post.borrow_id",'','/^\d+$/')!=''){
            $borrow_id=I("post.borrow_id");
            $borrowinfo=D("BorrowBuinessinfo");

            $borrow_array=$borrowinfo->borrow_info_detail_repaid_info($borrow_id);
            $this->ajaxReturn($borrow_array);

        }
    }
    /**
     * 分期借款业务详情， 订单信息 列表信息
     */
    public function ajax_borrow_info_detail_table(){
        if(IS_POST&&I("post.borrow_id",'','/^\d+$/')!=''){
            $borrow_id=I("post.borrow_id");
            $borrowinfo=D("BorrowBuinessinfo");

            $borrow_array=$borrowinfo->borrow_info_detail_table($borrow_id);
            $this->ajaxReturn($borrow_array);

        }
    }
	/**
	 *	借款业务详情#####设为还款动作
	 */
	public function borrowbusiness_detail_repay() 
	{
		if (IS_GET && I('get.repay_detail_id')) 
		{
			$date = date('Y-m-d H:i:s', time());
			$time = time();

			$model_capital_log = M('bypal_capital_log');
			$insert_capital_log['trade_type'] = '3';
			$insert_capital_log['business_id'] = I('get.binfo_id');
			$insert_capital_log['trade_describe'] = '分期订单:'.I('get.binfo_id').',还款期数：1,线下还款';
			$insert_capital_log['trade_money'] = I('get.money');
			$insert_capital_log['trade_status'] = '2';
			$insert_capital_log['cust_id'] = I('get.borrow_custid');
			$insert_capital_log['create_time'] = $date;

			$model_balance_invest = M('bypal_balance_invest');
			$data_balance_invest = $model_balance_invest->where('cust_id="'.I('get.borrow_custid').'"')->find();

			$model_capital_borrow = M('bypal_capital_borrow');
			$insert_capital_borrow['capital_date'] = $date;
			$insert_capital_borrow['capital_money'] = I('get.money');
			$insert_capital_borrow['capital_cust'] = I('get.borrow_custid');
			$insert_capital_borrow['capital_businessId'] = I('get.binfo_id');
			$insert_capital_borrow['capital_mold'] = '1';
			$insert_capital_borrow['capital_status'] = '0';
			$insert_capital_borrow['capital_type'] = '1';
			$insert_capital_borrow['balance_money'] = $data_balance_invest['fund_balance'] + I('get.money');

			$insert_capital_borrow2['capital_content'] = '还款订单:'.I('get.binfo_id').',还款期数：1,线下还款';
			$insert_capital_borrow2['capital_date'] = $date;
			$insert_capital_borrow2['capital_money'] = I('get.money');
			$insert_capital_borrow2['capital_cust'] = I('get.borrow_custid');
			$insert_capital_borrow2['capital_businessId'] = I('get.binfo_id');
			$insert_capital_borrow2['capital_mold'] = '4';
			$insert_capital_borrow2['capital_status'] = '0';
			$insert_capital_borrow2['capital_type'] = '2';
			$insert_capital_borrow2['balance_money'] = $data_balance_invest['fund_balance'] - I('get.money');

			$model_repay_detail = M('bypal_repay_detail');
			$update_repay_detail['repay_date'] = $time;
			$update_repay_detail['repaymtime'] = $time;
			$update_repay_detail['period_recovery'] = I('get.money');

			$count_repay_detail = $model_repay_detail->where('binfo_id="'.I('get.binfo_id').'" and repay_date=0')->count();

			$model_borrow_buinessinfo = M('bypal_borrow_buinessinfo');
			$data_borrow_buinessinfo = $model_borrow_buinessinfo->where('binfo_borrowid="'.I('get.binfo_id').'"')->find();
			if ($count_repay_detail == 0) 
			{
				$update_borrow_buinessinfo['buinfo_recentrepay'] = $time;
				$update_borrow_buinessinfo['binfo_remainmonth'] = '0';
				$update_borrow_buinessinfo['binfo_state'] = '4';
				$update_borrow_buinessinfo['buinfo_repaydown'] = $time;

				$model_facilitator_trade = M('bypal_facilitator_trade');
				$update_facilitator_trade['tid'] = '7';
				$model_facilitator_trade->where('trade_contractnumber="'.I('get.binfo_id').'"')->save($update_facilitator_trade);
			}
			else
			{
				$update_borrow_buinessinfo['buinfo_recentrepay'] = $time;
				$update_borrow_buinessinfo['binfo_remainmonth'] = $data_borrow_buinessinfo['binfo_remainmonth'] - 1;
			}
			if ($model_capital_log->add($insert_capital_log) && $model_capital_borrow->add($insert_capital_borrow) && $model_capital_borrow->add($insert_capital_borrow2) && $model_repay_detail->where('repay_detail_id="'.I('get.repay_detail_id').'"')->save($update_repay_detail) && $model_borrow_buinessinfo->where('binfo_borrowid="'.I('get.binfo_id').'"')->save($update_borrow_buinessinfo)) 
			{
				$result['state'] = '1';
				$result['message'] = "还款成功！";
			}
			else
			{
				$result['state'] = '2';
				$result['message'] = "网络错误，请重试或者联系客服";
			}

			$this->ajaxReturn(json_encode($result));
		}
	}


	/**
	 *	借款业务新增
	 */
	public function borrowbusiness_add() 
	{
		$this->display();
	}
}